package com.cloud.edu.sys1.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cloud.edu.frm.BaseController;
import com.cloud.edu.model.response.ObjectDataResponse;
import com.cloud.edu.sys.model.User;
import com.cloud.edu.sys.model.Dictionary;
import com.cloud.edu.sys.model.UserLoginLog;
//import com.cloud.edu.sys1.service.DictionaryService;
//import com.cloud.edu.sys1.service.ResourcesService;
//import com.cloud.edu.sys1.service.RoleService;
//import com.cloud.edu.sys1.service.UserLoginLogService;
//import com.cloud.edu.sys1.service.UserService;
import com.cloud.edu.utils.JsonUtils;
import com.cloud.edu.utils.StringUtils;


@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController{
//	
//	@Resource(name="userService1")
//	private UserService userService;
//	@Resource(name="userLoginLogService")
//	private UserLoginLogService userLoginLogService;
//	@Resource(name="resourcesService1")
//	private ResourcesService resourcesService;
//	@Resource(name="roleService1")
//	private RoleService roleService;
//	@Resource(name="dictionaryService1")
//	private DictionaryService dictionaryService;
//	
//	/**
//	 * 登陆
//	 * @param request
//	 * @param response
//	 * @return
//	 * @throws Exception
//	 */
//	@RequestMapping(value = "login", method = RequestMethod.GET)
//	public void login (HttpServletRequest request, HttpServletResponse response) throws Exception{
//		JSONObject returnObj = new JSONObject();
//		String loginName = request.getParameter("loginName");
//		String password = request.getParameter("password");
//		if (StringUtils.isNullString(loginName) || StringUtils.isNullString(password)) {
//			returnObj.element("message", "isNull");//用户名密码为空
//		}
//		// 想要得到 SecurityUtils.getSubject()　的对象．．访问地址必须跟shiro的拦截地址内．不然后会报空指针
//		Subject subject = SecurityUtils.getSubject();
//		// 用户输入的账号和密码,,存到UsernamePasswordToken对象中..然后由shiro内部认证对比,
//		// 认证执行者交由ShiroDbRealm中doGetAuthenticationInfo处理
//		// 当以上认证成功后会向下执行,认证失败会抛出异常
//		UsernamePasswordToken token = new UsernamePasswordToken(loginName, password);
//		try {
//			//通过Shiro进行用户名密码验证
//			subject.login(token);
//			
//			//将用户登陆信息保存到数据库
//			Session session = SecurityUtils.getSubject().getSession();
//			UserLoginLog userLoginLog = new UserLoginLog();
//			userLoginLog.setUserId((String)session.getAttribute("userSessionId"));
//			userLoginLog.setAccountName(loginName);
//			String userSessionId=(String)session.getAttribute("userSessionId");
//			userLoginLog.setLoginIp(session.getHost());
////			userLoginLogService.addUserLoginLog(userLoginLog);
//		
//			//登陆成功
//			returnObj.element("message", "success");//成功
//		} catch(UnknownAccountException uae){
//			token.clear();
//			returnObj.element("message", "usernotfound");//用户未找到
//		} catch(IncorrectCredentialsException ice){
//			ice.printStackTrace();
//			token.clear();
//			returnObj.element("message", "errorpwd");//密码错误
//		}catch (LockedAccountException lae) {
//			token.clear();
//			returnObj.element("message", "userlock");//用户已经被锁定,不能登录
//		} catch (ExcessiveAttemptsException e) {
//			token.clear();
//			returnObj.element("message", "failuremany");//登录失败次数过多
//		} catch (AuthenticationException e) {
//			token.clear();
//			returnObj.element("message", "usererror");//用户错误
//		}
//		super.responseJson(request, response, returnObj.toString());
//     }
//	
//	/**
//	 * 退出
//	 * @param request
//	 * @param response
//	 * @return
//	 * @throws Exception
//	 */
//	@RequestMapping(value ="logout", method = RequestMethod.GET)
//	public ModelAndView loginout(HttpServletRequest request,HttpServletResponse response) throws Exception {
//		ModelAndView modelView = new ModelAndView();
//		request.getSession().removeAttribute("systemUser");
//		modelView.setViewName("index");//跳转到首页面
//		return modelView;
//	}
//	
//	
//	
//	/**
//	 * 获取数据字典
//	 * @param request
//	 * @param response
//	 * @return
//	 * @throws Exception
//	 */
//	@RequestMapping(value ="getDicText", method = RequestMethod.GET)
//	public void getDicText(HttpServletRequest request,HttpServletResponse response) throws Exception {
//		String classCode = request.getParameter("classCode");
//		Dictionary dictionary = new Dictionary();
//		dictionary.setClassCode(classCode);
//		List<Dictionary> list = dictionaryService.getDictionary(dictionary);
//		super.responsePrint(request, response, JsonUtils.listToJson(list));
//	}
//	
//    @ApiOperation(value = "根据ID获取用户", notes = "111")
//    @RequestMapping(value = "/users/{userId}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
//    @ApiParam(required = true, name = "userId", value = "唯一标识")
//    public ObjectDataResponse<User> findUser(@PathVariable String userId) throws Exception {
//        User user = userService.getUserByID(userId);
//        if (user == null) {
////            Shift.fatal(StatusCode.USER_NOT_EXISTS);
//        }
//        ObjectDataResponse<User> user1=new ObjectDataResponse<>(user);
//        return user1;
//    }
//	
}
